export function getFormatDateTime(e) {
	const date = new Date(e) || new Date();
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1，并且保证是两位数  
	const day = String(date.getDate()).padStart(2, '0'); // 保证日期是两位数  
	const hours = String(date.getHours()).padStart(2, '0'); // 保证小时是两位数  
	var time = `${year}-${month}-${day}`;
	return time
}

// 当前时间的前一天
export function getFormatDatebeforeTime(e) {
	const date = new Date(e) || new Date();
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1，并且保证是两位数  
	const day = String(date.getDate() - 1).padStart(2, '0'); // 保证日期是两位数  
	const hours = String(date.getHours()).padStart(2, '0'); // 保证小时是两位数  
	var time = `${year}-${month}-${day}`;
	return time
}

export function getTimeStamp(e) {
	var date = new Date(e);
	date.setDate(date.getDate());
	return date;
}
export function getFormatDate(date) {
	if (!date) date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	if (hour >= 0 && hour <= 9) {
		hour = "0" + hour;
	}
	if (minute >= 0 && minute <= 9) {
		minute = "0" + minute;
	}
	if (second >= 0 && second <= 9) {
		second = "0" + second;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
		" " + hour + seperator2 + minute +
		seperator2 + second;
	return currentdate;
}

export function getBefore7DateTime(inputDate) {
	const baseDate = inputDate ? new Date(inputDate) : new Date();
	const targetDate = new Date(baseDate);
	targetDate.setDate(targetDate.getDate() - 7); // 修正为7天前
	const year = targetDate.getFullYear();
	const month = String(targetDate.getMonth() + 1).padStart(2, '0');
	const day = String(targetDate.getDate()).padStart(2, '0');
	return `${year}-${month}-${day}`;
}
export function formatDateTime(timeStr) {
	if (!timeStr) return '';
	return timeStr.replace('T', ' ').split('.')[0];
}
// base.js
export default {
	computed: {
		dictMaps() {
			return {

				MJWXS: this.createDictMap(this.MJWXSList),
				MJBFSQ: this.createDictMap(this.MJBFSQList),
				MJBFSQR: this.createDictMap(this.MJBFSQRList),
				MJBFS: this.createDictMap(this.MJBFSList),
				LYGHWS: this.createDictMap(this.LYGHWSList),
				SBWX: this.createDictMap(this.SBWXList),
				WXFS: this.createDictMap(this.WXFSList),
				MJZT: this.createDictMap(this.MJZTList),
				MaintenanceCycle: this.MaintenanceMap(this.MaintenanceCycleList),
				MaintenanceResults: this.MaintenanceMap(this.MaintenanceResultsList),
			};
		}
	},
	methods: {
		createDictMap(dictList) {
			const map = {};
			if (Array.isArray(dictList)) {
				dictList.forEach(item => {
					if (item.id !== undefined && item.name !== undefined) {
						map[item.id] = item.name;
					}
				});
			}
			return map;
		},
		MaintenanceMap(dictList) {
			const map = {};

			if (Array.isArray(dictList)) {
				dictList.forEach(item => {
					if (item.ID !== undefined && item.NAME !== undefined) {
						map[item.ID] = item.NAME;
					}
				});
			}

			return map;
		},

		async loadDictionary(type, key) {
			const that = this;

			const MesBasicInfo = that.$store.state.CRaftModule.MesBasicInfo;
			// 如果已有数据则不再请求
			if (MesBasicInfo[key] && MesBasicInfo[key].length > 0) {
				that[key] = MesBasicInfo[key];
				if (type === 'ALL') {
					that.getEnableApplyForNum()
				}
				return;
			}
			return new Promise((resolve) => {
				that.Request.request('MJ_Dictionary', {
					FType: type
				}, (ret) => {
					if (ret.data && ret.data.ds) {
						const data = type === 'ALL' ? ret.data.ds9 : ret.data.ds;
						that.$set(that, key, data);
						that.$set(MesBasicInfo, key, data);
						if (type === 'ALL') {

							that.getEnableApplyForNum()
						} else if (type == '保养结果') {
							that.MaintenanceResultsList = that.formatDictionaryData(data);
							that.$set(that, key, that.MaintenanceResultsList);
							that.$set(MesBasicInfo, key, that.MaintenanceResultsList);
						}
						that.$store.commit('CRaftModule/ChangeAndSetStateData', {
							Name1: 'MesBasicInfo',
							data: MesBasicInfo,
						});
					}
					resolve();
				});
			});
		},

		// 公共字典加载方法
		async loadCommonDictionaries(dictTypes) {

			await Promise.all(
				dictTypes.map(({
					type,
					key
				}) => this.loadDictionary(type, key))
			);
		},


		// 单个对象的字典映射版本
		mapDictNamesForItem(item, mappings) {
			const that = this;
			if (!item || typeof item !== 'object') return;
			Object.entries(mappings).forEach(([sourceField, config]) => {
				if (item[sourceField] != null) {
					const dictMap = that.dictMaps[config.dictKey];
					item[config.targetField || `${sourceField}Name`] = dictMap ?
						dictMap[item[sourceField]] || '' :
						'';
				}
			});
			return item;
		},
		/**
		 * 判断模具状态并设置相关参数
		 */
		getEnableApplyForNum() {
			let typeValue;
			// 详情页面优先使用 baseFormData
			if (this.baseFormData && this.baseFormData.FType !== undefined) {
				typeValue = this.baseFormData.FType;
			}
			// 列表页面使用 FormData
			else if (this.FormData && this.FormData.FType !== undefined) {
				typeValue = this.FormData.FType;
			}
			this.EnableApplyForNum = this.getMJZT(typeValue);
			this.EnableApplyForShow = this.EnableApplyForNum !== 1;
		},

		/**
		 * 获取模具状态值
		 * @param {String} val - 模具类型
		 * @returns {String|Number} - 返回对应的 EnableApplyFor 值
		 */
		getMJZT(val) {
			if (!this.MJZTList || this.MJZTList.length === 0) return '';
			const obj = this.MJZTList.find(item => item.FType == val);
			return obj ? obj.EnableApplyFor : '';
		},

		/**
		 * 格式化字典数据为 { text, value } 格式
		 * @param {Array} list 原始数据
		 * @returns {Array} 格式化后的数据
		 */
		formatDictionaryData(list) {
			return list.map(item => ({
				text: item.NAME,
				value: item.ID,
			}));
		},
	}
}